//
//  ProgressBarViewController.m
//  ProgressBar
//
//  Created by Luca Di Franco on 28/06/10.
//  Copyright Luca Di Franco 2010. All rights reserved.
//

#import "ProgressBarViewController.h"

@implementation ProgressBarViewController

#pragma mark -
#pragma mark View loading

- (void)viewDidLoad {
	// Run the heavy data processing in the background. Instruction here to be sure that the UIProgressView exists.
	[self performSelectorInBackground:@selector(heavyDataProcessing) withObject:nil];
}

#pragma mark -
#pragma mark UIProgressView management
																			 
// Executed in the background thread.
- (void)heavyDataProcessing {
	NSInteger numberOfTasks = 100;
	for (NSInteger tasksDone = 0; tasksDone <= numberOfTasks; tasksDone++) {
		// Heavy task processing simulation.
		[NSThread sleepForTimeInterval:0.1];
		NSNumber *progress = [[NSNumber alloc] initWithFloat:((float)tasksDone / (float)numberOfTasks)];
		// Update the progress view. On main thread to allow view refresh process.
		[self performSelectorOnMainThread:@selector(updateProgressBar:) withObject:progress waitUntilDone:NO];
		[progress release];
	}
}

// Executed in main thread.
- (void)updateProgressBar:(NSNumber *)progress {
	progressBar.progress = [progress floatValue];
}

#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)dealloc {
	[progressBar release];
    [super dealloc];
}

@end
